package com.example.compreshensiveshop.controller;

import com.example.compreshensiveshop.dto.CategoryAddDTO;
import com.example.compreshensiveshop.dto.CategoryDeleteDTO;
import com.example.compreshensiveshop.dto.CategoryUpdateDTO;
import com.example.compreshensiveshop.service.CategoryService;
import com.example.compreshensiveshop.vo.CategoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 获取所有公开分类
     */
    @RequestMapping("/getPublicCategory")
    public List<CategoryVO> getPublicCategory() {
        return categoryService.getPublicCategory();
    }

    /**
     * 获取所有商家分类
     */
    @GetMapping("/getSellerCategory/{sellerId}")
    public List<CategoryVO> getSellerCategory(@PathVariable Long sellerId) {
        return categoryService.getSellerCategory(sellerId);
    }

    /**
     * 添加分类
     */
    @RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
    public void addCategory(CategoryAddDTO categoryAddDTO) {
        categoryService.addCategory(categoryAddDTO);
    }

    /**
     * 修改分类
     */
    @PutMapping("/update")
    public void updateCategory(@RequestBody CategoryUpdateDTO categoryUpdateDTO) {
        categoryService.updateCategory(categoryUpdateDTO);
    }

    /**
     * 删除分类
     */
    @DeleteMapping("/delete")
    public String deleteCategory(@RequestBody CategoryDeleteDTO categoryDeleteDTO) {
        boolean success = categoryService.deleteCategory(categoryDeleteDTO);
        if (success) {
            return "删除成功";
        } else {
            return "删除失败：该分类下还有商品，无法删除";
        }
    }

}